package com.qilin.hscs.controller;


import com.qilin.hscs.dto.*;
import com.qilin.hscs.enums.ResultEnum;
import com.qilin.hscs.exception.SMException;
import com.qilin.hscs.form.BindForm;
import com.qilin.hscs.pojo.UserHost;
import com.qilin.hscs.service.UserHostService;
import com.qilin.hscs.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@RestController
@RequestMapping("/host")
public class UserHostController {

    @Autowired
    private UserHostService service;

    @RequestMapping("/bindHost")
    public ResultVO bindHost(@Valid @RequestBody BindForm form, BindingResult result) {
        if (result.hasErrors()) {
            throw new SMException(ResultEnum.PARAM_ERROR.getCode(), result.getFieldError().getDefaultMessage());
        }
        UserHostBindDto dto = new UserHostBindDto();
        BeanUtils.copyProperties(form,dto);
        return service.bindHost(dto);
    }

    @GetMapping("/getUserHost")
    public ResultVO findHostsByUserPhone(String phone){
        return service.findHostsByUserPhone(phone);
    }

    @GetMapping("/adminHostUser")
    public ResultVO findUsersInHost(String mac){
            return service.findUsersInHost(mac);
    }

    @PostMapping("/allotRoles")
    public ResultVO allotRoles(@RequestBody AllotRolesDto dto){
        return service.allotRoles(dto);
    }

    @PostMapping("/transferAdmin")
    public ResultVO transferAdmin(@RequestBody TransferAdminDto dto){
        return service.transferAdmin(dto);
    }

    @PostMapping("/switchHost")
    public ResultVO switchHost(@RequestBody SwitchHostDto dto){
        return service.switchHost(dto);
    }

    @PostMapping("/releaseHost")
    public ResultVO releaseHost(@RequestBody ReleaseHostDto dto){
        return service.releaseHost(dto);
    }

}
